class Lianxian extends egret.DisplayObjectContainer {
    private context: any;
    private speed = 0.2;
    private text: any;
    public constructor(context: any, text: any) {
        super();
        this.context = context;
        this.text = text;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {
        new Bg(this, 'mask_in_png');
        var percentage = 0.6;
        // 加载主面板
        var imgCon = [];
        imgCon['url'] = "i_" + this.text + '_png'
        imgCon['width'] = this.stage.stageWidth * percentage;
        imgCon['height'] = this.stage.stageHeight * percentage;
        imgCon['y'] = this.stage.stageHeight * (1 - percentage) / 2;
        imgCon['x'] = this.stage.stageWidth * (1 - percentage) / 2;
        this.addChild(new ImageBitmap(imgCon).getImgPoint());

        var _syb = new eui.Image();
        _syb.source = this.text;
        _syb.width = 100;
        _syb.height = _syb.width;
        _syb.x = 305;
        _syb.y = 265;
        this.addChild(_syb);

        //增加关闭按钮
        this.setClose();
        this.alpha = 0;
        this.openAm();

    }

    public openAm(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
        this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
    }
    public closeAm(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.closeFrameHandler, this);
        this.addEventListener(egret.Event.ENTER_FRAME, this.closeFrameHandler, this);
    }
    /**打开时逐帧运动*/
    private enterFrameHandler(event: egret.Event): void {
        if (this.alpha < 1) {
            this.alpha += this.speed;
        } else {
            this.pauseOpenAm();
        }
    }

    /**
     * 关闭时动画运动
     * @param event
     */
    private closeFrameHandler(event: egret.Event): void {
        if (this.alpha > 0) {
            this.alpha -= this.speed;
        } else {
            this.pauseOpenAm();
            if (this.parent) {
                this.parent.removeChild(this);
            }
        }
    }
    /**暂停打开时动画*/
    public pauseOpenAm(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
    }

    /**暂停关闭时的动画*/
    public pauseCloseAm(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.closeFrameHandler, this);
    }

    private onClose(evt: egret.TouchEvent) {
        this.closeAm();
    }
    private setClose(): void {
        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
    }

}